/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.Carts;
import com.liuzian.marketserver.entity.Commodity;
import com.liuzian.marketserver.mapper.CartsMapper;
import com.liuzian.marketserver.service.CartsService;
import com.liuzian.marketserver.service.CommodityService;
import com.liuzian.marketserver.service.ImageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CartsServiceImpl extends ServiceImpl<CartsMapper, Carts> implements CartsService {

    @Lazy
    @Resource
    private CommodityService commodityService;

    @Lazy
    @Resource
    private ImageService imageService;


    @Override
    public R<String> addCarts(Carts carts) {
        log.info("get form data {}", carts);
        super.save(carts);
        return R.success("添加购物车成功!");
    }

    @Override
    public R<Carts> queryCarts(Long cid) {
        LambdaQueryWrapper<Carts> cartsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartsLambdaQueryWrapper.eq(Carts::getCid, cid);
        Carts carts = super.getOne(cartsLambdaQueryWrapper);
        carts.setCommodity(commodityService.getCommodityByGid(carts.getGid()));
        return R.success(carts);
    }

    @Override
    public R<List<Carts>> queryAllCarts(Long uid) {
        LambdaQueryWrapper<Carts> cartsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartsLambdaQueryWrapper.eq(Carts::getUid, uid);
        cartsLambdaQueryWrapper.orderByDesc(Carts::getCid);
        List<Carts> cartsList = super.list(cartsLambdaQueryWrapper);
        for (Carts carts : cartsList) {
            Commodity commodity = commodityService.getCommodityByGid(carts.getGid());
            commodity.setImage(imageService.getImageByGid(commodity.getGid()));
            carts.setCommodity(commodity);
        }
        return R.success(cartsList);
    }

    @Override
    public R<String> updateCarts(int count, double price, Long cid) {
        LambdaUpdateWrapper<Carts> cartsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        cartsLambdaUpdateWrapper.set(Carts::getCount, count);
        cartsLambdaUpdateWrapper.set(Carts::getPrice, price);
        cartsLambdaUpdateWrapper.eq(Carts::getCid, cid);
        boolean bool = super.update(cartsLambdaUpdateWrapper);
        if (bool) {
            return R.success("商品信息更新成功");
        } else {
            return R.error("商品信息更新失败");
        }
    }

    @Override
    public R<String> deleteCarts(Long cid) {
        LambdaQueryWrapper<Carts> cartsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartsLambdaQueryWrapper.eq(Carts::getCid, cid);
        boolean bool = super.remove(cartsLambdaQueryWrapper);
        if (bool) {
            return R.success("successful");
        } else {
            return R.error("failed");
        }
    }
}
